package net.xdclass.util;


/**
 * @Classname DoubleLinke
 * @Description TODO
 * @Date 2022/11/24 19:42
 * @Created by sunhao
 */
public class DoubleLinke {

  public static void main(String[] args) {
    CustmeLinke2 CustmeLinke2 = new CustmeLinke2();
    CustmeLinke2.addCourse(new CourseNode2(1, "class1"));
    CustmeLinke2.addCourse(new CourseNode2(2, "class2"));
    CustmeLinke2.addCourse(new CourseNode2(3, "class3"));
    CustmeLinke2.showNode();
  }
}


class CustmeLinke2 {
  /**
   * 初始化头部节点
   */
  private CourseNode2 head = new CourseNode2(0, "");
  /**
   * 有效节点个数
   */
  private int length = 0;

  /**
   * 添加元素节点
   */
  public void addCourse(CourseNode2 courseNode) {
    CourseNode2 cur = head;
    while (true) {
      //寻找最后一个节点
      if (cur.next == null) {
        break;
      }
      cur = cur.next;
    }
    cur.next = courseNode;
    cur.next.pre = cur;
    length++;
  }

  /**
   * 编辑节点
   */
  public void showNode() {
    CourseNode2 cur = head;
    if (cur.next == null) {
      System.out.println("链表无元素");
      return;
    }
    while (true) {
      if (cur.next == null) {
        System.out.println("链表输出完成!");
        return;
      }
      System.out.println(cur.next);
      cur = cur.next;
    }
  }

  /**
   * 根据Id 删除节点
   */
  public void deleteCourse(int id) {
    CourseNode2 cur = head.next;
    boolean flag = false;
    while (cur != null) {
      if (cur.id == id) {
        flag = true;
        break;
      }
      cur = cur.next;
    }
    if (flag) {
       cur.pre.next = cur.next;
       if(cur.next != null){
         cur.next.pre=cur.pre;
       }
      length--;
    } else {
      System.out.printf("没有找到要删除的元素", id);
    }
  }

  /**
   * 根据Id 修改节点
   */
  public void updateCourse(int id, CourseNode2 courseNode) {
    CourseNode2 cur = head;
    boolean flag = false;
    while (cur.next != null) {
      if (cur.next.id == id) {
        flag = true;
        break;
      }
      cur = cur.next;
    }
    if (flag) {
      cur.next.id = courseNode.id;
      cur.next.courseName = courseNode.courseName;
    } else {
      System.out.printf("没有找到要修改的元素!",id);
    }
  }

  /**
   * 获取元素个数
   */
  public int getLength(){
    return length;
  }

  /**
   * 获取倒数第K个元素
   */
  public CourseNode2 getLastCourse(int k){
    CourseNode2 cur = head;
    if(cur.next == null){
      System.out.println("没有可查找的元素!");
      return null;
    }
    int length = getLength();
    if(length < k){
      System.out.println("index out of range");
      return null;
    }
    for (int i = 0; i < length-k ; i++) {
      cur = cur.next;
    }
    return cur.next;
  }
}
class CourseNode2 {
  /**
   * 节点Id
   */
  public int id;

  /**
   * 节点名称
   */
  public String courseName;

  /**
   * 下一节点指针
   */
  public CourseNode2 next;

  /**
   * 前驱节点
   */
  public CourseNode2 pre;

  public CourseNode2(int id, String courseName) {
    this.id = id;
    this.courseName = courseName;
    this.next = null;
    this.pre = null;
  }

  @Override
  public String toString() {
    return "CourseNode2{" +
        "id=" + id +
        ", courseName='" + courseName + '\'' +
        ", next=" + next +
        ", previous=" + pre +
        '}';
  }
}



